﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jy123.Web.AppCode;

namespace Jy123.Web.Manage.User.Gift
{
    public partial class Add : MgConfig//System.Web.UI.Page
    {
        protected string oldpic = string.Empty;
        AppCode.CreateDir createDir = new Jy123.Web.AppCode.CreateDir();
        string photoAllPath = string.Empty;
        int giftID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Add.aspx");
            if (!IsPostBack)
            {
                BindddlClass();

                if (Request.QueryString["ID"] != null)
                {
                    giftID = int.Parse(Request.QueryString["ID"]);
                    Jy123.Model.Public_Gift modelGift = new Jy123.Model.Public_Gift();
                    Jy123.BLL.Public_Gift bllGift = new Jy123.BLL.Public_Gift();
                    modelGift = bllGift.GetModel(giftID);
                    gName.Text = modelGift.GName;
                    this.hidPic.Value = modelGift.Picture;
                    //fpicture.FileName = modelGift.Picture;
                    price.Text = modelGift.Price.ToString();
                    txtContent.Text = modelGift.Decription;
                    ddlClass.SelectedValue = modelGift.FGiftClass.ToString();

                }
            }
        }


        public void BindddlClass()
        {
            Jy123.Model.Public_GiftClass modelGClass = new Jy123.Model.Public_GiftClass();
            Jy123.BLL.Public_GiftClass bllGClass = new Jy123.BLL.Public_GiftClass();
            ddlClass.DataSource = bllGClass.GetList("");
            ddlClass.DataTextField = "GiftClassName";
            ddlClass.DataValueField = "ID";
            ddlClass.DataBind();
            //ddlClass.SelectedIndex = 0;
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            Jy123.Model.Public_Gift modelGift = new Jy123.Model.Public_Gift();
            Jy123.BLL.Public_Gift bllGift = new Jy123.BLL.Public_Gift();

            if (Request.QueryString["ID"] != null)
            {
                giftID = int.Parse(Request.QueryString["id"]);

                modelGift = bllGift.GetModel(giftID);
                modelGift.GName = gName.Text;
                modelGift.Price = decimal.Parse(price.Text);
                modelGift.Decription = txtContent.Text;
                modelGift.FGiftClass = int.Parse(ddlClass.SelectedValue);
                if (fpicture.FileName.Length == 0)
                {
                    modelGift.Picture = hidPic.Value;
                }
                else
                {
                    UpdatePhoto();
                    modelGift.Picture = photoAllPath;
                }
                bllGift.Update(modelGift);
                Response.Redirect("Index.aspx");
            }
            else
            {
                if (Page.IsValid)
                {
                    UpdatePhoto();
                    modelGift = bllGift.GetModel(giftID);
                    modelGift.Picture = photoAllPath;
                    bllGift.Update(modelGift);
                    Response.Redirect("Index.aspx");
                }
            }
        }
        /// <summary>
        /// 更新图片的路径
        /// </summary>
        private void UpdatePhoto()
        {
            bool fileOK = false;
            Common.Upload upload = new Jy123.Common.Upload();
            HttpPostedFile postFile = fpicture.PostedFile;
            string photoPath = postFile.FileName;


            if (fpicture.HasFile)
            {

                if (upload.IsPic(photoPath))
                {
                    fileOK = true;
                }
                else
                {
                    Response.Write("文件必须为.gif, .png, .bmp, .jpg等类型");
                }
                if (fileOK)
                {
                    if (giftID != 0)
                    {
                        //Jy123.Model.Public_Gift modelGift = new Jy123.Model.Public_Gift();
                        ////new Jy123.BLL.Public_Gift().GetModel();
                        string photoType = photoPath.Substring(photoPath.LastIndexOf(".") + 1);
                        //modelGift.GName = gName.Text;
                        //modelGift.Price = int.Parse(price.Text);
                        //modelGift.Decription = txtContent.Text;
                        //modelGift.FGiftClass = int.Parse(ddlClass.SelectedValue);

                        //giftID = new Jy123.BLL.Public_Gift().Add(modelGift);

                        photoAllPath = createDir.CreateGiftPath(CreateDir.GIFT, giftID, photoType);
                        string mapPath = Server.MapPath("/");

                        upload.PostedFile = postFile;
                        upload.FileLength = postFile.ContentLength;
                        upload.FileName = photoAllPath;
                        upload.Extension = photoType;
                        upload.SavePath = mapPath;
                        upload.UploadStart(photoAllPath);
                    }
                    else
                    {
                        Jy123.Model.Public_Gift modelGift = new Jy123.Model.Public_Gift();
                        //new Jy123.BLL.Public_Gift().GetModel();
                        string photoType = photoPath.Substring(photoPath.LastIndexOf(".") + 1);
                        modelGift.GName = gName.Text;
                        modelGift.Price = int.Parse(price.Text);
                        modelGift.Decription = txtContent.Text;
                        modelGift.FGiftClass = int.Parse(ddlClass.SelectedValue);

                        giftID = new Jy123.BLL.Public_Gift().Add(modelGift);

                        photoAllPath = createDir.CreateGiftPath(CreateDir.GIFT, giftID, photoType);
                        string mapPath = Server.MapPath("/");

                        upload.PostedFile = postFile;
                        upload.FileLength = postFile.ContentLength;
                        upload.FileName = photoAllPath;
                        upload.Extension = photoType;
                        upload.SavePath = mapPath;
                        upload.UploadStart(photoAllPath);
                    }

                }
            }
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        private void UploadPhoto()
        {
            bool fileOK = false;
            Common.Upload upload = new Jy123.Common.Upload();
            HttpPostedFile postFile = fpicture.PostedFile;
            string photoPath = postFile.FileName;


            if (fpicture.HasFile)
            {

                if (upload.IsPic(photoPath))
                {
                    fileOK = true;
                }
                else
                {
                    Response.Write("文件必须为.gif, .png, .bmp, .jpg等类型");
                }
                if (fileOK)
                {
                    Jy123.Model.Public_Gift modelGift = new Jy123.Model.Public_Gift();
                    string photoType = photoPath.Substring(photoPath.LastIndexOf(".") + 1);
                    modelGift.GName = gName.Text;
                    modelGift.Price = int.Parse(price.Text);
                    modelGift.Decription = txtContent.Text;
                    modelGift.FGiftClass = int.Parse(ddlClass.SelectedValue);

                    giftID = new Jy123.BLL.Public_Gift().Add(modelGift);

                    photoAllPath = createDir.CreateGiftPath(CreateDir.GIFT, giftID, photoType);
                    string mapPath = Server.MapPath("/");

                    upload.PostedFile = postFile;
                    upload.FileLength = postFile.ContentLength;
                    upload.FileName = photoAllPath;
                    upload.Extension = photoType;
                    upload.SavePath = mapPath;
                    upload.UploadStart(photoAllPath);

                }
            }
        }

        protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                ddlClass.DataBind();
            }
        }
    }
}
